import React, { PureComponent } from 'react';
import { connect } from 'dva';
import router from 'umi/router';
import { Card, Row, Col, Icon, Modal, List, Button, Spin, Input, Popconfirm, Empty, Tag, Divider } from 'antd';
import PageHeaderWrapper from '@/components/PageHeaderWrapper';

const { Meta } = Card;

@connect(({ competition }) => ({
  competition: competition.competition,
}))
class Competition extends PureComponent {


  componentDidMount() {
    const { match: { params: { id } }, dispatch } = this.props;
    dispatch({
      type: 'competition/getCompetitionDetail',
      payload: {
        competitionId: id,
      }
    })
  }

  handlePreview = () => {
    const { match: { params: { id } } } = this.props;
    router.push(`/preview/competition/${id}/paper`)
  }

  onDisable = (competition) => {
    if (competition&&competition.paperVo&&competition.competitionVo.status===3) {
      const date = new Date(competition.paperVo.examStartDate);
      const endDate = new Date(competition.paperVo.examEndDate);
      const today = new Date();
      // console.log(today)
      if (today >= date && today <= endDate) {
        return false;
      } else {
        return true;
      }
    }
    return true;
  }

  onResultDisable = (competition) =>{
    if (competition&&competition.paperVo) {
      const date = new Date(competition.paperVo.announceAnswer);
      const today = new Date();
      console.log(date,today)
      if (today >= date) {
        return false;
      } else {
        return true;
      }
    }
    return true;
  }

  handleLook = () =>{
    const { match: { params: { id } } } = this.props;
    router.push(`/preview/competition/${id}/charts`)
  }


  render() {
    const { competition } = this.props;
    console.log(competition)
    return (
      <PageHeaderWrapper>
        <Card
          style={{ height: 600 }}
        >
          <Row>
            <h1>{competition && competition.competitionInformationVo && competition.competitionInformationVo.name}</h1>
          </Row>
          {competition&&competition.competitionVo&&competition.competitionVo.status===3?
          <Row>
            上传作品开始时间：{competition && competition.paperVo && competition.paperVo.examStartDate.slice(0, 10)}
            <br/>
            上传作品结束时间：{competition && competition.paperVo && competition.paperVo.examEndDate.slice(0, 10)}
            <br/>
            时间段内可提交作品
            <br/>
            <br/>
            <h2>现在时间是：</h2>
            <h3>{new Date().toLocaleDateString()}</h3>
          </Row>
          :
          <Row>
            <h1>竞赛已结束</h1>
          </Row>
          }
          
          <Row>
            <Button disabled={this.onDisable(competition&&competition)} onClick={this.handlePreview}>提交作品</Button>
          </Row>
          <Divider />
          <Row>
            查看竞赛结果时间：{competition && competition.paperVo && competition.paperVo.announceAnswer.slice(0, 10)}
            <br/>
            <br/>
          </Row>
          <Row>
            <Button
              // disabled={this.onDisable(competition && competition.paperVo)}
              onClick={this.handleLook}
              type="primary"
              disabled={this.onResultDisable(competition&&competition)}
            >
              查看排行
            </Button>
          </Row>
        </Card>
      </PageHeaderWrapper>
    );
  }
}

export default Competition;
